package com.googlecode.c4e.ui;

import java.net.URL;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

import org.eclipse.core.runtime.Platform;
import org.eclipse.core.runtime.Status;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.ui.plugin.AbstractUIPlugin;
import org.osgi.framework.BundleContext;

import com.googlecode.c4e.api.ProjectStates;
import com.googlecode.c4e.api.model.Project;
import com.googlecode.c4e.api.model.ProjectGroup;
import com.googlecode.c4e.api.model.Server;
import com.googlecode.c4e.ui.views.internal.C4EAdapterFactory;

public class ContinuumPlugin extends AbstractUIPlugin {

	public static final String PLUGIN_ID = "com.googlecode.c4e.ui";

	private static ContinuumPlugin plugin;

	private Map<String, ImageDescriptor> imageDescriptors;

	private URL baseUrl;

	public ContinuumPlugin() {
		plugin = this;
	}

	public static ContinuumPlugin getDefault() {
		return plugin;
	}

	public void start(BundleContext context) throws Exception {
		super.start(context);
		baseUrl = context.getBundle().getEntry("/");
		Map<String, ImageDescriptor> result = new HashMap<String, ImageDescriptor>();
		try {
			C4EAdapterFactory adapterFactory = new C4EAdapterFactory();
			Platform.getAdapterManager().registerAdapters(adapterFactory,
					Project.class);
			Platform.getAdapterManager().registerAdapters(adapterFactory,
					ProjectGroup.class);
			Platform.getAdapterManager().registerAdapters(adapterFactory,
					Server.class);

			result
					.put("state" + ProjectStates.NEW, ImageDescriptor
							.createFromURL(new URL(baseUrl,
									"icons/state/success.gif")));
			result
					.put("state" + ProjectStates.OK, ImageDescriptor
							.createFromURL(new URL(baseUrl,
									"icons/state/success.gif")));
			result
					.put("state" + ProjectStates.UPDATED, ImageDescriptor
							.createFromURL(new URL(baseUrl,
									"icons/state/success.gif")));
			result
					.put("state" + ProjectStates.WARNING, ImageDescriptor
							.createFromURL(new URL(baseUrl,
									"icons/state/warning.gif")));
			result
					.put("state" + ProjectStates.FAILED, ImageDescriptor
							.createFromURL(new URL(baseUrl,
									"icons/state/warning.gif")));
			result.put("state" + ProjectStates.ERROR, ImageDescriptor
					.createFromURL(new URL(baseUrl, "icons/state/error.gif")));
			result.put("state" + ProjectStates.BUILDING,
					ImageDescriptor.createFromURL(new URL(baseUrl,
							"icons/state/building.gif")));
			result.put("state" + ProjectStates.CHECKING_OUT, ImageDescriptor
					.createFromURL(new URL(baseUrl,
							"icons/state/checkingout.gif")));
			result.put("state" + ProjectStates.UPDATING, ImageDescriptor
					.createFromURL(new URL(baseUrl,
							"icons/state/checkingout.gif")));

			result.put("continuum", ImageDescriptor.createFromURL(new URL(
					baseUrl, "icons/continuum.gif")));
		} catch (Exception e) {
			e.printStackTrace();
		}
		imageDescriptors = Collections.unmodifiableMap(result);
	}

	public void stop(BundleContext context) throws Exception {
		plugin = null;
		super.stop(context);
	}

	public ImageDescriptor getImageDescriptorForProjectState(Integer state) {
		return imageDescriptors.get("state" + state);
	}

	public ImageDescriptor getImageDescriptor(String name) {
		return imageDescriptors.get(name);
	}

	public void log(String msg) {
		log(msg, null);
	}

	public void log(String msg, Exception e) {
		if (e == null) {
			getLog().log(
					new Status(Status.ERROR, PLUGIN_ID, Status.ERROR, msg, e));
		} else {
			getLog().log(new Status(Status.INFO, PLUGIN_ID, Status.OK, msg, e));
		}
	}

}
